package com.bjpowernode.crm.workbench.service.impl;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.mapper.UserMapper;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.workbench.domain.Activity;
import com.bjpowernode.crm.workbench.dto.ActivityQueryDto;
import com.bjpowernode.crm.workbench.mapper.ActivityMapper;
import com.bjpowernode.crm.workbench.service.ActivityService;
import com.bjpowernode.crm.workbench.vo.ActivityQueryVo;
import lombok.Getter;
import lombok.Setter;

import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Getter
@Setter
public class ActivityServiceImpl implements ActivityService {

    /**
     * 访问市场活动数据
     */
    private ActivityMapper activityMapper;

    /**
     * 处理用户相关业务
     */
    private UserService userService;

    @Override
    public ReturnObject addActivity(Activity activity, HttpSession session) {
        User currentUser = (User) session.getAttribute(Contants.SESSION_USER);
        String now = DateUtils.formatDateTime(new Date());

        activity.setId(UUIDUtils.getUUID());
        activity.setCreateTime(now);
        activity.setCreateBy(currentUser.getId());
        activity.setEditTime(now);
        activity.setEditBy(currentUser.getId());
        this.activityMapper.insertActivity(activity);

        ReturnObject returnObject = new ReturnObject();
        returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        returnObject.setMessage("恭喜，保存成功");
        return returnObject;
    }

    @Override
    public ActivityQueryVo queryActivitiesByConditionsForPage(ActivityQueryDto activityQueryDto) {
//前台提供的是所有人的名称，数据库中存放的是所有人的 id
        if (activityQueryDto.getOwner() != null && activityQueryDto.getOwner().length() > 0) {
            //根据前台传入的所有者名称，模糊查询所有者 id 列表，再根据所有者 id 列表搜索市场活动
            activityQueryDto.setIdOfOwners(this.userService.queryIdsByName(activityQueryDto.getOwner()));
        }

        ActivityQueryVo activityQueryVo = new ActivityQueryVo();
        activityQueryVo.setActivityList(this.activityMapper.selectActivitiesByConditionsForPage(activityQueryDto));

        if (activityQueryVo.getActivityList() != null && activityQueryVo.getActivityList().size() > 0) {
            //设置总条数
            activityQueryVo.setTotalRows(this.activityMapper.selectCountOfActivitiesByConditions(activityQueryDto));

            //在数据库中，存储的是所有者、创建者、最后编辑者的 id ，结果中需要的是他们的名称
            for (Activity activity : activityQueryVo.getActivityList()) {
                activity.setOwner(this.userService.queryNameById(activity.getOwner()));
                activity.setCreateBy(this.userService.queryNameById(activity.getCreateBy()));
                activity.setEditBy(this.userService.queryNameById(activity.getEditBy()));
            }
        }

        return activityQueryVo;
    }

    @Override
    public int queryCountOfActivitiesByConditions(ActivityQueryDto activityQueryDto) {
        return this.activityMapper.selectCountOfActivitiesByConditions(activityQueryDto);
    }

    @Override
    public void removeActivitiesByIds(String[] ids) {
        this.activityMapper.deleteActivitiesByIds(ids);
    }

    @Override
    public Activity queryActivityById(String id) {
        Activity activity = this.activityMapper.selectActivityById(id);
        activity.setOwner(this.userService.queryNameById(activity.getOwner()));
        activity.setCreateBy(this.userService.queryNameById(activity.getCreateBy()));
        activity.setEditBy(this.userService.queryNameById(activity.getEditBy()));
        return activity;
    }

    @Override
    public ReturnObject editActivityById(Activity activity, HttpSession session) {
        User currentUser = (User) session.getAttribute(Contants.SESSION_USER);

        activity.setEditTime(DateUtils.formatDateTime(new Date()));
        activity.setEditBy(currentUser.getId());
        this.activityMapper.updateActivityById(activity);

        ReturnObject returnObject = new ReturnObject();
        returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        returnObject.setMessage("恭喜，更新成功");
        return returnObject;
    }

    @Override
    public List<Activity> queryActivityByClueId(String clueId) {
        List<Activity> activities = this.activityMapper.selectActivityByClueId(clueId);

        //在数据库中，存储的是所有者、创建者、最后编辑者的 id ，结果中需要的是他们的名称
        for (Activity activity : activities) {
            activity.setOwner(this.userService.queryNameById(activity.getOwner()));
            activity.setCreateBy(this.userService.queryNameById(activity.getCreateBy()));
            activity.setEditBy(this.userService.queryNameById(activity.getEditBy()));
        }

        return activities;
    }

    @Override
    public List<Activity> queryActivitiesByIdsOfExclusions(List<String> idsOfExcludedActivities, String name) {
        List<Activity> activities = this.activityMapper.selectActivitiesByIdsOfExclusions(idsOfExcludedActivities, name);

        //数据库中存放的是所有者、创建者、最后一个编辑者的 id ，页面需要的是他们的名称
        for (Activity activity : activities) {
            activity.setOwner(this.userService.queryNameById(activity.getOwner()));
            activity.setCreateBy(this.userService.queryNameById(activity.getCreateBy()));
            activity.setEditBy(this.userService.queryNameById(activity.getEditBy()));
        }

        return activities;
    }

    @Override
    public String queryNameById(String id) {
        return this.activityMapper.selectNameById(id);
    }

    @Override
    public List<Activity> queryActivitiesByIds(String[] ids) {
        List<Activity> activities = this.activityMapper.selectActivitiesByIds(ids);

        //数据库中存放的是所有者、创建者、最后一个编辑者的 id ，页面需要的是他们的名称
        for (Activity activity : activities) {
            activity.setOwner(this.userService.queryNameById(activity.getOwner()));
            activity.setCreateBy(this.userService.queryNameById(activity.getCreateBy()));
            activity.setEditBy(this.userService.queryNameById(activity.getEditBy()));
        }

        return activities;
    }

    @Override
    public List<Activity> queryActivitiesByIdsAndName(String name, List<String> ids) {
        return this.activityMapper.selectActivitiesByIdsAndName(name, ids);
    }

}
